package com.brillilab.service.logic.kit;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import com.brillilab.common.alioss.OssFileEnum;
import com.brillilab.common.entity.PageVo;
import com.brillilab.common.utils.MyBeanUtils;
import com.brillilab.domain.enums.kit.KitEnum;
import com.brillilab.domain.pa.kit.KitModifyPa;
import com.brillilab.domain.pa.kit.KitReagentSpecAddPa;
import com.brillilab.domain.pa.kit.KitSpecAddPa;
import com.brillilab.domain.pa.kit.ReagentAddMorePa;
import com.brillilab.domain.pa.kit.ReagentAntibodyPa;
import com.brillilab.domain.pa.kit.ReagentCurrencyPa;
import com.brillilab.domain.pa.kit.ReagentModifyPa;
import com.brillilab.domain.pa.kit.ReagentPlasmidPa;
import com.brillilab.domain.pa.kit.ReagentPrimerPa;
import com.brillilab.domain.pa.kit.ReagentSearchPa;
import com.brillilab.domain.po.inventory.ReagentInventory;
import com.brillilab.domain.po.kit.Kit;
import com.brillilab.domain.po.kit.KitSpec;
import com.brillilab.domain.po.kit.Reagent;
import com.brillilab.domain.po.kit.ReagentAntibody;
import com.brillilab.domain.po.kit.ReagentPlasmid;
import com.brillilab.domain.po.kit.ReagentPlasmidFragment;
import com.brillilab.domain.po.kit.ReagentPlasmidUse;
import com.brillilab.domain.po.kit.ReagentPrimer;
import com.brillilab.domain.po.kit.ReagentPrimerAttr;
import com.brillilab.domain.po.system.FileManage;
import com.brillilab.domain.vo.kit.KitInfoListVo;
import com.brillilab.domain.vo.kit.ReagentDetailsVo;
import com.brillilab.domain.vo.kit.ReagentInfoVo;
import com.brillilab.domain.vo.kit.ReagentKitSpecVo;
import com.brillilab.domain.vo.kit.ReagentSpecVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.constant.ServiceConstant;
import com.brillilab.service.core.elsearch.IEsKitSaveService;
import com.brillilab.service.core.inventory.IReagentInventoryService;
import com.brillilab.service.core.kit.IKitService;
import com.brillilab.service.core.kit.IKitSpecService;
import com.brillilab.service.core.kit.IReagentAntibodyService;
import com.brillilab.service.core.kit.IReagentPlasmidFragmentService;
import com.brillilab.service.core.kit.IReagentPlasmidService;
import com.brillilab.service.core.kit.IReagentPrimerAttrService;
import com.brillilab.service.core.kit.IReagentPrimerService;
import com.brillilab.service.core.kit.IReagentService;
import com.brillilab.service.core.kit.IReagentTypeService;
import com.brillilab.service.core.lab.ILabMemberService;
import com.brillilab.service.core.system.IDictService;
import com.brillilab.service.core.system.IFileManageService;

/**
 * 试剂Logic
 * 
 * @author lj
 *
 */
@Service
@Transactional
public class ReagentLogic {

	@Resource
	private IKitService kitService;
	@Resource
	private IKitSpecService kitSpecService;
	@Resource
	private IReagentService reagentService;
	@Resource
	private IReagentAntibodyService reagentAntibodyService;
	@Resource
	private IReagentPrimerService reagentPrimerService;
	@Resource
	private IReagentPrimerAttrService reagentPrimerAttrService;
	@Resource
	private IReagentPlasmidService reagentPlasmidService;
	@Resource
	private IReagentPlasmidFragmentService reagentPlasmidFragmentService;
	@Resource
	private IDictService dictService;
	@Resource
	private IReagentInventoryService reagentInventoryService;
	@Resource
	private ILabMemberService labMemberService;
	@Resource
	private IFileManageService fileManageService;
	@Resource
	private IEsKitSaveService esKitSaveService;
	@Resource
	private IReagentTypeService reagentTypeService;

	/**
	 * 添加通用类型试剂
	 * 
	 * @param pa
	 * @return kitId
	 */
	public Long addCurrencyReagent(ReagentCurrencyPa pa) {
		if (pa.getReagentTypeId() == null) {
			pa.setReagentTypeId(ServiceConstant.other_reagent_type_id);
		}
		// 1、保存Kit信息
		Kit kit = MyBeanUtils.copyBean(pa, Kit.class);
		kit.setId(null);
		kit.setLabId(pa.getLabId());
		kit.setCreatorId(pa.getCreatorId());
		kit.setClassify(KitEnum.Classify.REAGENT.getValue());
		kit.setKitType(KitEnum.KitType.ONE.getValue());
		kit = kitService.insert(kit);
		// 1.1保存kit附件
		if (!CollectionUtils.isEmpty(pa.getInstructionFileList())) {
			for (FileManage file : pa.getInstructionFileList()) {
				fileManageService.insertBatch(file.getPath(), kit.getId(), OssFileEnum.KitInstructionFile,
						kit.getLabId(), null);
			}
		}
		if (pa.getTempFileIds() != null && pa.getTempFileIds().length > 0) {
			fileManageService.relevanceBatch(pa.getTempFileIds(), kit.getId(), OssFileEnum.KitInstructionFile,
					kit.getLabId(), null);
		}

		// 2、保存试剂信息
		Reagent reagent = MyBeanUtils.copyBean(kit, Reagent.class);
		reagent.setId(null);
		reagent.setKitId(kit.getId());
		reagent.setConcentration(pa.getConcentration());
		reagent.setNumeratorUnit(pa.getNumeratorUnit());
		reagent.setDenominatorUnit(pa.getDenominatorUnit());
		reagent = reagentService.insert(reagent);

		// 3、保存规格信息
		KitSpec kitSpec = new KitSpec();
		kitSpec.setLabId(kit.getLabId());
		kitSpec.setKitId(kit.getId());
		kitSpec.setReagentId(reagent.getId());
		kitSpec.setUnit(pa.getUnit());
		kitSpec.setKitSize(pa.getSize().multiply(BigDecimal.valueOf(pa.getMultiple())));
		kitSpec.setSize(pa.getSize());
		kitSpec.setMultiple(pa.getMultiple());
		kitSpec = kitSpecService.insert(kitSpec);
		esKitSaveService.kitSave(kit.getId());
		return kit.getId();
	}

	/**
	 * 新增抗体类型试剂
	 * 
	 * @param pa
	 * @return kitId
	 */
	public Long addAntibodyReagent(ReagentAntibodyPa pa) {
		ReagentCurrencyPa currency = pa.getReagentCurrencyPa();
		ReagentAntibody antibody = pa.getReagentAntibody();

		// 1、保存Kit信息
		Kit kit = MyBeanUtils.copyBean(currency, Kit.class);
		kit.setId(null);
		kit.setLabId(currency.getLabId());
		kit.setCreatorId(currency.getCreatorId());
		kit.setReagentTypeId(ServiceConstant.antibody_reagent_type_id);
		kit.setClassify(KitEnum.Classify.REAGENT.getValue());
		kit.setKitType(KitEnum.KitType.ONE.getValue());
		kit = kitService.insert(kit);
		// 1.1保存Kit附件
		if (!CollectionUtils.isEmpty(currency.getInstructionFileList())) {
			for (FileManage file : currency.getInstructionFileList()) {
				fileManageService.insertBatch(file.getPath(), kit.getId(), OssFileEnum.KitInstructionFile,
						kit.getLabId(), null);
			}
		}
		if (currency.getTempFileIds() != null && currency.getTempFileIds().length > 0) {
			fileManageService.relevanceBatch(currency.getTempFileIds(), kit.getId(), OssFileEnum.KitInstructionFile,
					kit.getLabId(), null);
		}
		// 2、保存试剂信息
		Reagent reagent = MyBeanUtils.copyBean(kit, Reagent.class);
		reagent.setId(null);
		reagent.setLabId(kit.getLabId());
		reagent.setKitId(kit.getId());
		reagent.setConcentration(currency.getConcentration());
		reagent.setNumeratorUnit(currency.getNumeratorUnit());
		reagent.setDenominatorUnit(currency.getDenominatorUnit());
		reagent = reagentService.insert(reagent);

		// 3、保存规格信息
		KitSpec kitSpec = new KitSpec();
		kitSpec.setLabId(kit.getLabId());
		kitSpec.setKitId(kit.getId());
		kitSpec.setReagentId(reagent.getId());
		kitSpec.setUnit(currency.getUnit());
		kitSpec.setKitSize(currency.getSize().multiply(BigDecimal.valueOf(currency.getMultiple())));
		kitSpec.setSize(currency.getSize());
		kitSpec.setMultiple(currency.getMultiple());
		kitSpec = kitSpecService.insert(kitSpec);

		// 4、保存抗体属性
		if (antibody != null) {
			antibody.setKitId(kit.getId());
			antibody.setReagentId(reagent.getId());
			antibody = reagentAntibodyService.insert(antibody);
		}
		esKitSaveService.kitSave(kit.getId());
		return kit.getId();
	}

	/**
	 * 新增引物类型试剂
	 * 
	 * @param pa
	 * @return kitId
	 */
	public Long addPrimerReagent(ReagentPrimerPa pa) {
		ReagentCurrencyPa currency = pa.getReagentCurrencyPa();
		ReagentPrimer primer = pa.getReagentPrimer();
		List<ReagentPrimerAttr> attrList = pa.getReagentPrimerAttrList();

		// 1、保存Kit信息
		Kit kit = MyBeanUtils.copyBean(currency, Kit.class);
		kit.setId(null);
		kit.setName(currency.getName());
		kit.setLabId(currency.getLabId());
		kit.setCreatorId(currency.getCreatorId());
		kit.setReagentTypeId(ServiceConstant.primer_reagent_type_id);
		kit.setClassify(KitEnum.Classify.REAGENT.getValue());
		kit.setKitType(KitEnum.KitType.ONE.getValue());
		kit = kitService.insert(kit);
		// 1.1保存Kit附件
		if (!CollectionUtils.isEmpty(currency.getInstructionFileList())) {
			for (FileManage file : currency.getInstructionFileList()) {
				fileManageService.insertBatch(file.getPath(), kit.getId(), OssFileEnum.KitInstructionFile,
						kit.getLabId(), null);
			}
		}
		if (currency.getTempFileIds() != null && currency.getTempFileIds().length > 0) {
			fileManageService.relevanceBatch(currency.getTempFileIds(), kit.getId(), OssFileEnum.KitInstructionFile,
					kit.getLabId(), null);
		}
		// 2、保存试剂信息
		Reagent reagent = MyBeanUtils.copyBean(kit, Reagent.class);
		reagent.setId(null);
		reagent.setName(kit.getName());
		reagent.setLabId(kit.getLabId());
		reagent.setKitId(kit.getId());
		reagent.setConcentration(currency.getConcentration());
		reagent.setNumeratorUnit(currency.getNumeratorUnit());
		reagent.setDenominatorUnit(currency.getDenominatorUnit());
		reagent = reagentService.insert(reagent);

		// 3、保存规格信息
		KitSpec kitSpec = MyBeanUtils.copyBean(currency, KitSpec.class);
		kitSpec.setId(null);
		kitSpec.setLabId(kit.getLabId());
		kitSpec.setKitId(kit.getId());
		kitSpec.setReagentId(reagent.getId());
		kitSpec.setKitSize(currency.getSize().multiply(BigDecimal.valueOf(currency.getMultiple())));
		kitSpec = kitSpecService.insert(kitSpec);

		// 4、保存引物信息
		if (primer != null) {
			primer.setKitId(kit.getId());
			primer.setReagentId(reagent.getId());
			primer = reagentPrimerService.insert(primer);
			// 5、保存引物信息属性
			if (!CollectionUtils.isEmpty(attrList)) {
				for (ReagentPrimerAttr primerAttr : attrList) {
					primerAttr.setKitId(kit.getId());
					primerAttr.setReagentId(reagent.getId());
					primerAttr.setName(primerAttr.getName());
					reagentPrimerAttrService.insert(primerAttr);
				}
			}
		}
		esKitSaveService.kitSave(kit.getId());
		return kit.getId();
	}

	/**
	 * 新增质粒类型试剂
	 * 
	 * @param pa
	 * @return kitId
	 */
	public Long addPlasmidReagent(ReagentPlasmidPa pa) {
		ReagentCurrencyPa currency = pa.getReagentCurrencyPa();
		ReagentPlasmid plasmid = pa.getReagentPlasmid();
		List<ReagentPlasmidFragment> fragmentList = pa.getReagentPlasmidFragmentList();

		// 1、保存Kit信息
		Kit kit = MyBeanUtils.copyBean(currency, Kit.class);
		kit.setId(null);
		kit.setLabId(currency.getLabId());
		kit.setCreatorId(currency.getCreatorId());
		kit.setReagentTypeId(ServiceConstant.plasmid_reagent_type_id);
		kit.setClassify(KitEnum.Classify.REAGENT.getValue());
		kit.setKitType(KitEnum.KitType.ONE.getValue());
		kit = kitService.insert(kit);
		// 1.1保存Kit附件
		if (!CollectionUtils.isEmpty(currency.getInstructionFileList())) {
			for (FileManage file : currency.getInstructionFileList()) {
				fileManageService.insertBatch(file.getPath(), kit.getId(), OssFileEnum.KitInstructionFile,
						kit.getLabId(), null);
			}
		}
		if (currency.getTempFileIds() != null && currency.getTempFileIds().length > 0) {
			fileManageService.relevanceBatch(currency.getTempFileIds(), kit.getId(), OssFileEnum.KitInstructionFile,
					kit.getLabId(), null);
		}
		// 2、保存试剂信息
		Reagent reagent = MyBeanUtils.copyBean(kit, Reagent.class);
		reagent.setId(null);
		reagent.setLabId(kit.getLabId());
		reagent.setKitId(kit.getId());
		reagent.setConcentration(currency.getConcentration());
		reagent.setNumeratorUnit(currency.getNumeratorUnit());
		reagent.setDenominatorUnit(currency.getDenominatorUnit());
		reagent = reagentService.insert(reagent);

		// 3、保存规格信息
		KitSpec kitSpec = new KitSpec();
		kitSpec.setLabId(kit.getLabId());
		kitSpec.setKitId(kit.getId());
		kitSpec.setReagentId(reagent.getId());
		kitSpec.setUnit(currency.getUnit());
		kitSpec.setKitSize(currency.getSize().multiply(BigDecimal.valueOf(currency.getMultiple())));
		kitSpec.setSize(currency.getSize());
		kitSpec.setMultiple(currency.getMultiple());
		kitSpec = kitSpecService.insert(kitSpec);

		// 4、保存质粒使用记录
		Long plasmidTemplateId = plasmid.getId();
		if (plasmidTemplateId != null) {
			ReagentPlasmidUse plasmidUse = reagentPlasmidService.selectUseOne(reagent.getLabId(), plasmidTemplateId);
			if (plasmidUse != null) {
				plasmidUse.setUseTime(new Date());
				reagentPlasmidService.updateUseById(plasmidUse);
			} else {
				ReagentPlasmid plasmid2 = reagentPlasmidService.selectById(plasmidTemplateId);// 质粒模版数据
				if (plasmid2 != null) {
					plasmidUse = new ReagentPlasmidUse();
					plasmidUse.setLabId(kit.getLabId());
					plasmidUse.setReagentPlasmidId(plasmid2.getId());
					plasmidUse.setReagentPlasmidName(plasmid2.getPlasmidName());
					plasmidUse.setType(plasmid2.getLabId() != null ? 2 : 1);
					plasmidUse.setUseTime(new Date());
					reagentPlasmidService.insertUse(plasmidUse);
				}
			}
		}

		// 5、保存质粒信息
		if (plasmid != null) {

			plasmid.setId(null);
			plasmid.setLabId(kit.getLabId());
			plasmid.setKitId(kit.getId());
			plasmid.setReagentId(reagent.getId());
			plasmid.setPlasmidName(reagent.getName());
			plasmid = reagentPlasmidService.insert(plasmid);

			// 5.1图谱文件
			if (plasmid.getAtlasFileIds() != null && plasmid.getAtlasFileIds().length > 0) {
				fileManageService.relevanceBatch(plasmid.getAtlasFileIds(), plasmid.getId(),
						OssFileEnum.PlasmidAtlasFile, kit.getLabId(), null);
			}
			if (!CollectionUtils.isEmpty(plasmid.getAtlasFileList())) {
				for (FileManage file : plasmid.getAtlasFileList()) {
					fileManageService.insertBatch(file.getPath(), plasmid.getId(), OssFileEnum.PlasmidAtlasFile,
							kit.getLabId(), null);
				}
			}
			// 测序报告文件
			if (plasmid.getReportFileIds() != null && plasmid.getReportFileIds().length > 0) {
				fileManageService.relevanceBatch(plasmid.getReportFileIds(), plasmid.getId(),
						OssFileEnum.PlasmidReportFile, kit.getLabId(), null);
			}
			if (!CollectionUtils.isEmpty(plasmid.getReportFileList())) {
				for (FileManage file : plasmid.getReportFileList()) {
					fileManageService.insertBatch(file.getPath(), plasmid.getId(), OssFileEnum.PlasmidReportFile,
							kit.getLabId(), null);
				}
			}
			// 序列文摘文件
			if (plasmid.getAbstractFileIds() != null && plasmid.getAbstractFileIds().length > 0) {
				fileManageService.relevanceBatch(plasmid.getAbstractFileIds(), plasmid.getId(),
						OssFileEnum.PlasmidAbstractFile, kit.getLabId(), null);
			}
			if (!CollectionUtils.isEmpty(plasmid.getAbstractFileList())) {
				for (FileManage file : plasmid.getAbstractFileList()) {
					fileManageService.insertBatch(file.getPath(), plasmid.getId(), OssFileEnum.PlasmidAbstractFile,
							kit.getLabId(), null);
				}
			}
			// 其他文件
			if (plasmid.getOtherFileIds() != null && plasmid.getOtherFileIds().length > 0) {
				fileManageService.relevanceBatch(plasmid.getOtherFileIds(), plasmid.getId(),
						OssFileEnum.PlasmidOtherFile, kit.getLabId(), null);
			}
			if (!CollectionUtils.isEmpty(plasmid.getOtherFileList())) {
				for (FileManage file : plasmid.getOtherFileList()) {
					fileManageService.insertBatch(file.getPath(), plasmid.getId(), OssFileEnum.PlasmidOtherFile,
							kit.getLabId(), null);
				}
			}

			// 6、保存质粒片段
			if (!CollectionUtils.isEmpty(fragmentList)) {
				for (ReagentPlasmidFragment fragment : fragmentList) {
					fragment.setKitId(kit.getId());
					fragment.setReagentId(reagent.getId());
					reagentPlasmidFragmentService.insert(fragment);
				}
			}
		}
		esKitSaveService.kitSave(kit.getId());
		return kit.getId();
	}

	/**
	 * 新增多成分试剂
	 * 
	 * @param pa
	 * @return kitId
	 */
	public Long addMoreReagent(ReagentAddMorePa pa) {
		ReagentCurrencyPa currency = pa.getReagentCurrencyPa();
		if (currency.getReagentTypeId() == null) {
			currency.setReagentTypeId(ServiceConstant.other_reagent_type_id);
		}
		// 1、保存Kit信息
		Kit kit = MyBeanUtils.copyBean(currency, Kit.class);
		kit.setId(null);
		kit.setLabId(currency.getLabId());
		kit.setCreatorId(currency.getCreatorId());
		kit.setClassify(KitEnum.Classify.REAGENT.getValue());
		kit.setKitType(KitEnum.KitType.MORE.getValue());
		kit = kitService.insert(kit);
		// 1.1保存Kit附件
		if (!CollectionUtils.isEmpty(currency.getInstructionFileList())) {
			for (FileManage file : currency.getInstructionFileList()) {
				fileManageService.insertBatch(file.getPath(), kit.getId(), OssFileEnum.KitInstructionFile,
						kit.getLabId(), null);
			}
		}
		if (currency.getTempFileIds() != null && currency.getTempFileIds().length > 0) {
			fileManageService.relevanceBatch(currency.getTempFileIds(), kit.getId(), OssFileEnum.KitInstructionFile,
					kit.getLabId(), null);
		}

		BigDecimal kitSize = currency.getSize();

		// 通用试剂列表
		List<KitSpec> saveSpecList = new ArrayList<KitSpec>();
		List<ReagentCurrencyPa> currencyList = pa.getReagentCurrencyList();
		if (!CollectionUtils.isEmpty(currencyList)) {
			for (ReagentCurrencyPa currencyReagent : currencyList) {
				if (currencyReagent.getReagentTypeId() == null) {
					currencyReagent.setReagentTypeId(ServiceConstant.other_reagent_type_id);
				}
				// 保存试剂信息
				Reagent reagent = MyBeanUtils.copyBean(currencyReagent, Reagent.class);
				reagent.setId(null);
				reagent.setClassify(KitEnum.Classify.REAGENT.getValue());
				reagent.setLabId(kit.getLabId());
				reagent.setKitId(kit.getId());
				reagent.setConcentration(currencyReagent.getConcentration());
				reagent.setNumeratorUnit(currencyReagent.getNumeratorUnit());
				reagent.setDenominatorUnit(currencyReagent.getDenominatorUnit());
				reagent = reagentService.insert(reagent);

				// 1.1保存Reagent附件
				if (!CollectionUtils.isEmpty(currency.getInstructionFileList())) {
					for (FileManage file : currency.getInstructionFileList()) {
						if (file.getId() == null || file.getId() == 0)
							fileManageService.insertBatch(file.getPath(), reagent.getId(),
									OssFileEnum.ReagentInstructionFile, kit.getLabId(), null);
					}
				}
				if (currencyReagent.getTempFileIds() != null && currencyReagent.getTempFileIds().length > 0) {
					fileManageService.relevanceBatch(currencyReagent.getTempFileIds(), reagent.getId(),
							OssFileEnum.ReagentInstructionFile, kit.getLabId(), null);
				}

				// 保存规格信息
				KitSpec kitSpec = new KitSpec();
				kitSpec.setLabId(kit.getLabId());
				kitSpec.setKitId(kit.getId());
				kitSpec.setReagentId(reagent.getId());
				kitSpec.setKitSize(kitSize);
				kitSpec.setUnit(currencyReagent.getUnit());
				kitSpec.setSize(currencyReagent.getSize());
				kitSpec.setMultiple(currencyReagent.getMultiple());
				saveSpecList.add(kitSpec);
			}
		}
		// 抗体试剂列表
		List<ReagentAntibodyPa> antibodyList = pa.getReagentAntibodyList();
		if (!CollectionUtils.isEmpty(antibodyList)) {
			for (ReagentAntibodyPa reagentAntibodyPa : antibodyList) {
				// 保存试剂信息
				ReagentCurrencyPa reagentCurrency = reagentAntibodyPa.getReagentCurrencyPa();
				Reagent reagent = MyBeanUtils.copyBean(reagentCurrency, Reagent.class);
				reagent.setId(null);
				reagent.setReagentTypeId(ServiceConstant.antibody_reagent_type_id);
				reagent.setClassify(KitEnum.Classify.REAGENT.getValue());
				reagent.setLabId(kit.getLabId());
				reagent.setKitId(kit.getId());
				reagent.setConcentration(reagentCurrency.getConcentration());
				reagent.setNumeratorUnit(reagentCurrency.getNumeratorUnit());
				reagent.setDenominatorUnit(reagentCurrency.getDenominatorUnit());
				reagent = reagentService.insert(reagent);
				// 1.1保存附件
				if (!CollectionUtils.isEmpty(currency.getInstructionFileList())) {
					for (FileManage file : currency.getInstructionFileList()) {
						fileManageService.insertBatch(file.getPath(), reagent.getId(),
								OssFileEnum.ReagentInstructionFile, kit.getLabId(), null);
					}
				}
				if (reagentCurrency.getTempFileIds() != null && reagentCurrency.getTempFileIds().length > 0) {
					fileManageService.relevanceBatch(reagentCurrency.getTempFileIds(), reagent.getId(),
							OssFileEnum.ReagentInstructionFile, reagent.getLabId(), null);
				}
				// 保存规格信息
				KitSpec kitSpec = new KitSpec();
				kitSpec.setLabId(kit.getLabId());
				kitSpec.setKitId(kit.getId());
				kitSpec.setReagentId(reagent.getId());
				kitSpec.setKitSize(kitSize);
				kitSpec.setUnit(reagentCurrency.getUnit());
				kitSpec.setSize(reagentCurrency.getSize());
				kitSpec.setMultiple(reagentCurrency.getMultiple());
				saveSpecList.add(kitSpec);

				// 保存抗体属性
				ReagentAntibody antibody = reagentAntibodyPa.getReagentAntibody();
				if (antibody != null) {
					antibody.setKitId(kit.getId());
					antibody.setReagentId(reagent.getId());
					antibody = reagentAntibodyService.insert(antibody);
				}
			}
		}
		// 质粒试剂列表
		List<ReagentPlasmidPa> plasmidList = pa.getReagentPlasmidList();
		if (!CollectionUtils.isEmpty(plasmidList)) {
			for (ReagentPlasmidPa reagentPlasmidPa : plasmidList) {
				// 保存试剂信息
				ReagentCurrencyPa reagentCurrency = reagentPlasmidPa.getReagentCurrencyPa();
				Reagent reagent = MyBeanUtils.copyBean(reagentCurrency, Reagent.class);
				reagent.setId(null);
				reagent.setReagentTypeId(ServiceConstant.plasmid_reagent_type_id);
				reagent.setClassify(KitEnum.Classify.REAGENT.getValue());
				reagent.setLabId(kit.getLabId());
				reagent.setKitId(kit.getId());
				reagent.setConcentration(reagentCurrency.getConcentration());
				reagent.setNumeratorUnit(reagentCurrency.getNumeratorUnit());
				reagent.setDenominatorUnit(reagentCurrency.getDenominatorUnit());
				reagent = reagentService.insert(reagent);

				// 1.1保存附件
				if (!CollectionUtils.isEmpty(currency.getInstructionFileList())) {
					for (FileManage file : currency.getInstructionFileList()) {
						fileManageService.insertBatch(file.getPath(), reagent.getId(),
								OssFileEnum.ReagentInstructionFile, kit.getLabId(), null);
					}
				}
				if (reagentCurrency.getTempFileIds() != null && reagentCurrency.getTempFileIds().length > 0) {
					fileManageService.relevanceBatch(reagentCurrency.getTempFileIds(), reagent.getId(),
							OssFileEnum.ReagentInstructionFile, reagent.getLabId(), null);
				}

				// 保存规格信息
				KitSpec kitSpec = new KitSpec();
				kitSpec.setLabId(kit.getLabId());
				kitSpec.setKitId(kit.getId());
				kitSpec.setReagentId(reagent.getId());
				kitSpec.setKitSize(kitSize);
				kitSpec.setUnit(reagentCurrency.getUnit());
				kitSpec.setSize(reagentCurrency.getSize());
				kitSpec.setMultiple(reagentCurrency.getMultiple());
				saveSpecList.add(kitSpec);

				// 保存质粒信息
				ReagentPlasmid plasmid = reagentPlasmidPa.getReagentPlasmid();
				if (plasmid != null) {
					// 4、保存质粒使用记录
					Long plasmidTemplateId = plasmid.getId();
					if (plasmidTemplateId != null) {
						ReagentPlasmidUse plasmidUse = reagentPlasmidService.selectUseOne(reagent.getLabId(),
								plasmidTemplateId);
						if (plasmidUse != null) {
							plasmidUse.setUseTime(new Date());
							reagentPlasmidService.updateUseById(plasmidUse);
						} else {
							ReagentPlasmid plasmid2 = reagentPlasmidService.selectById(plasmidTemplateId);// 质粒模版数据
							if (plasmid2 != null) {
								plasmidUse = new ReagentPlasmidUse();
								plasmidUse.setLabId(kit.getLabId());
								plasmidUse.setReagentPlasmidId(plasmid2.getId());
								plasmidUse.setReagentPlasmidName(plasmid2.getPlasmidName());
								plasmidUse.setType(plasmid2.getLabId() != null ? 2 : 1);
								plasmidUse.setUseTime(new Date());
								reagentPlasmidService.insertUse(plasmidUse);
							}
						}
					}

					// 保存质粒信息
					plasmid.setPlasmidName(reagent.getName());
					plasmid.setLabId(kit.getLabId());
					plasmid.setKitId(kit.getId());
					plasmid.setReagentId(reagent.getId());
					plasmid = reagentPlasmidService.insert(plasmid);

					// 5.1图谱文件
					if (plasmid.getAtlasFileIds() != null && plasmid.getAtlasFileIds().length > 0) {
						fileManageService.relevanceBatch(plasmid.getAtlasFileIds(), plasmid.getId(),
								OssFileEnum.PlasmidAtlasFile, kit.getLabId(), null);
					}
					if (!CollectionUtils.isEmpty(plasmid.getAtlasFileList())) {
						for (FileManage file : plasmid.getAtlasFileList()) {
							fileManageService.insertBatch(file.getPath(), plasmid.getId(), OssFileEnum.PlasmidAtlasFile,
									kit.getLabId(), null);
						}
					}
					// 测序报告文件
					if (plasmid.getReportFileIds() != null && plasmid.getReportFileIds().length > 0) {
						fileManageService.relevanceBatch(plasmid.getReportFileIds(), plasmid.getId(),
								OssFileEnum.PlasmidReportFile, kit.getLabId(), null);
					}
					if (!CollectionUtils.isEmpty(plasmid.getReportFileList())) {
						for (FileManage file : plasmid.getReportFileList()) {
							fileManageService.insertBatch(file.getPath(), plasmid.getId(),
									OssFileEnum.PlasmidReportFile, kit.getLabId(), null);
						}
					}
					// 序列文摘文件
					if (plasmid.getAbstractFileIds() != null && plasmid.getAbstractFileIds().length > 0) {
						fileManageService.relevanceBatch(plasmid.getAbstractFileIds(), plasmid.getId(),
								OssFileEnum.PlasmidAbstractFile, kit.getLabId(), null);
					}
					if (!CollectionUtils.isEmpty(plasmid.getAbstractFileList())) {
						for (FileManage file : plasmid.getAbstractFileList()) {
							fileManageService.insertBatch(file.getPath(), plasmid.getId(),
									OssFileEnum.PlasmidAbstractFile, kit.getLabId(), null);
						}
					}
					// 其他文件
					if (plasmid.getOtherFileIds() != null && plasmid.getOtherFileIds().length > 0) {
						fileManageService.relevanceBatch(plasmid.getOtherFileIds(), plasmid.getId(),
								OssFileEnum.PlasmidOtherFile, kit.getLabId(), null);
					}
					if (!CollectionUtils.isEmpty(plasmid.getOtherFileList())) {
						for (FileManage file : plasmid.getOtherFileList()) {
							fileManageService.insertBatch(file.getPath(), plasmid.getId(), OssFileEnum.PlasmidOtherFile,
									kit.getLabId(), null);
						}
					}

					// 5、保存质粒片段
					List<ReagentPlasmidFragment> fragmentList = reagentPlasmidPa.getReagentPlasmidFragmentList();
					if (!CollectionUtils.isEmpty(fragmentList)) {
						for (ReagentPlasmidFragment fragment : fragmentList) {
							fragment.setKitId(kit.getId());
							fragment.setReagentId(reagent.getId());
							reagentPlasmidFragmentService.insert(fragment);
						}
					}
				}
			}
		}
		// 批量插入规格
		kitSpecService.insertBatch(saveSpecList);
		esKitSaveService.kitSave(kit.getId());
		return kit.getId();
	}

	/**
	 * 试剂列表分页
	 * 
	 * @return
	 */
	public PageVo<KitInfoListVo> kitList(ReagentSearchPa pa, UserInfoVo user) {
		return kitService.reagentList(pa, user.getLabId(), user.getLabMemberId());
	}

	/**
	 * 获取试剂详情
	 * 
	 * @param kitId
	 * @return
	 */
	public ReagentDetailsVo getReagentDetails(Long kitId) {
		ReagentDetailsVo vo = null;
		Kit kit = kitService.selectById(kitId);
		if (kit != null) {
			Map<Integer, String> unitMap = dictService.getUnitMap();
			Map<Integer, String> zoneMap = dictService.getZoneMap();
			Map<Long, String> reagentTypeMap = dictService.getReagentTypeMap(kit.getLabId());
			vo = MyBeanUtils.copyBean(kit, ReagentDetailsVo.class);
			vo.setIsLabExist(1);
			vo.setKitId(kit.getId());

			vo.setReagentTypeName(reagentTypeMap.get(kit.getReagentTypeId()));
			vo.setUnitName(unitMap.get(kit.getUnit()));
			vo.setZoneName(zoneMap.get(kit.getZone()));
			List<KitSpec> kitSpecList = kitSpecService.selectSpecGroup(kit.getId());
			String kitSpecName = kitSpecService.getSpecName(kitSpecList, 0);
			vo.setSpecName(kitSpecName);
			List<FileManage> fileList = fileManageService.selectList(OssFileEnum.KitInstructionFile, kit.getId());
			vo.setInstructionFileList(fileList);

			List<ReagentInfoVo> reagentListVo = new ArrayList<>();
			List<Reagent> reagentList = reagentService.selectList(kitId);

			List<Long> reagentTypeIds = reagentList.stream().map(Reagent::getReagentTypeId)
					.collect(Collectors.toList());
			reagentTypeIds.add(kit.getReagentTypeId());
			Map<Long, Long> firstReagentTypeMap = reagentTypeService.selectFirstReagentTypeMap(reagentTypeIds);
			vo.setFirstReagentTypeId(firstReagentTypeMap.get(kit.getReagentTypeId()));

			reagentList.forEach(reagent -> {
				// 组装reagent信息数据
				ReagentInfoVo reagentVo = MyBeanUtils.copyBean(reagent, ReagentInfoVo.class);
				reagentVo.setIsLabExist(1);
				reagentVo.setReagentId(reagent.getId());
				reagentVo.setZoneName(zoneMap.get(reagent.getZone()));
				reagentVo.setReagentTypeName(reagentTypeMap.get(reagent.getReagentTypeId()));
				reagentVo.setUnitName(unitMap.get(reagent.getUnit()));
				reagentVo.setNumeratorUnitName(unitMap.get(reagent.getNumeratorUnit()));
				reagentVo.setDenominatorUnitName(unitMap.get(reagent.getDenominatorUnit()));
				reagentVo.setFirstReagentTypeId(firstReagentTypeMap.get(reagent.getReagentTypeId()));

				if (KitEnum.KitType.ONE.getValue().equals(kit.getKitType())) {
					reagentVo.setInstructionFileList(fileList);
				} else {
					List<FileManage> fileList2 = fileManageService.selectList(OssFileEnum.ReagentInstructionFile,
							reagent.getId());
					reagentVo.setInstructionFileList(fileList2);
				}

				// 组装reagent规格
				List<KitSpec> specList = kitSpecService.selectSpecList(kit.getId(), reagent.getId());
				String specName = kitSpecService.getSpecName(specList, 1);
				reagentVo.setSpecName(specName);

				if (reagent.getReagentTypeId().equals(ServiceConstant.antibody_reagent_type_id)) {
					ReagentAntibody antibody = reagentAntibodyService.selectOne(reagent.getKitId(), reagent.getId());
					reagentVo.setReagentAntibody(antibody);
				} else if (reagent.getReagentTypeId().equals(ServiceConstant.primer_reagent_type_id)) {
					ReagentPrimer reagentPrimer = reagentPrimerService.selectOne(reagent.getKitId(), reagent.getId());
					reagentVo.setReagentPrimer(reagentPrimer);
				} else if (reagent.getReagentTypeId().equals(ServiceConstant.plasmid_reagent_type_id)) {
					ReagentPlasmid reagentPlasmid = reagentPlasmidService.selectOne(reagent.getKitId(),
							reagent.getId());
					reagentVo.setReagentPlasmid(reagentPlasmid);
				}
				reagentListVo.add(reagentVo);
			});
			vo.setReagentList(reagentListVo);
		}
		return vo;
	}

	/**
	 * 新增试剂规格表
	 * 
	 * @param pa
	 * @return
	 */
	public Long addReagentSpec(KitSpecAddPa pa) {
		// 实验室试剂
		Kit kit = kitService.selectById(pa.getKitId());
		if (kit.getKitType() == 0) {// 单成分
			KitReagentSpecAddPa reagentSpec = pa.getKitReagentSpecList().get(0);
			Assert.isTrue(reagentSpec.getSize() != null && reagentSpec.getSize().compareTo(BigDecimal.ZERO) == 1,
					"试剂规格必须大于0");
			Assert.isTrue(reagentSpec.getMultiple() != null && reagentSpec.getMultiple() > 0, "试剂支数必须大于0");

			BigDecimal kitSize = reagentSpec.getSize().multiply(BigDecimal.valueOf(reagentSpec.getMultiple()));
			List<KitSpec> specList = kitSpecService.selectList(pa.getKitId(), kitSize);
			Assert.isTrue(specList == null || specList.size() == 0, "已存在相同规格");

			KitSpec kitSpec = new KitSpec();
			kitSpec.setLabId(pa.getLabId());
			kitSpec.setKitId(pa.getKitId());
			kitSpec.setKitSize(kitSize);
			kitSpec.setReagentId(reagentSpec.getReagentId());
			kitSpec.setUnit(reagentSpec.getUnit());
			kitSpec.setSize(reagentSpec.getSize());
			kitSpec.setMultiple(reagentSpec.getMultiple());
			kitSpec = kitSpecService.insert(kitSpec);
		} else {// 多成分
			List<KitSpec> specList = kitSpecService.selectList(pa.getKitId(), pa.getKitSize());
			Assert.isTrue(specList == null || specList.size() == 0, "已存在相同规格");
			List<KitReagentSpecAddPa> reagentSpecList = pa.getKitReagentSpecList();
			Assert.isTrue(pa.getKitSize() != null && pa.getKitSize().compareTo(BigDecimal.ZERO) == 1, "试剂规格必须大于0");
			List<KitSpec> saveSpecList = new ArrayList<KitSpec>();
			for (KitReagentSpecAddPa reagentSpec : reagentSpecList) {
				Assert.isTrue(reagentSpec.getSize() != null && reagentSpec.getSize().compareTo(BigDecimal.ZERO) == 1,
						"试剂规格必须大于0");
				Assert.isTrue(reagentSpec.getMultiple() != null && reagentSpec.getMultiple() > 0, "试剂支数必须大于0");

				KitSpec kitSpec = new KitSpec();
				kitSpec.setLabId(pa.getLabId());
				kitSpec.setKitId(pa.getKitId());
				kitSpec.setKitSize(pa.getKitSize());
				kitSpec.setReagentId(reagentSpec.getReagentId());
				kitSpec.setUnit(reagentSpec.getUnit());
				kitSpec.setSize(reagentSpec.getSize());
				kitSpec.setMultiple(reagentSpec.getMultiple());
				saveSpecList.add(kitSpec);
			}
			// 批量插入规格
			kitSpecService.insertBatch(saveSpecList);
		}
		return kit.getId();
	}

	/**
	 * 获取试剂规格
	 * 
	 * @param kitId
	 * @return
	 */
	public List<ReagentKitSpecVo> getReagentSpec(Long kitId) {
		List<ReagentKitSpecVo> listVo = new ArrayList<>();

		Kit kit = kitService.selectById(kitId);

		Map<String, ReagentKitSpecVo> map = new HashMap<>();
		Map<Integer, String> unitMap = dictService.getUnitMap();
		Map<Integer, String> zoneMap = dictService.getZoneMap();
		Map<Long, String> reagentTypeMap = dictService.getReagentTypeMap(kit.getLabId());
		List<KitSpec> specList = kitSpecService.selectList(kitId, null);
		List<Reagent> reagentList = reagentService.selectList(kitId);
		Map<Long, List<Reagent>> reagentMap = reagentList.stream().collect(Collectors.groupingBy(Reagent::getId));

		List<Long> reagentTypeIds = reagentList.stream().map(Reagent::getReagentTypeId).collect(Collectors.toList());
		reagentTypeIds.add(kit.getReagentTypeId());
		Map<Long, Long> firstReagentTypeMap = reagentTypeService.selectFirstReagentTypeMap(reagentTypeIds);

		specList.forEach(kitSpec -> {
			// 组装reagent信息数据
			Reagent reagent = reagentMap.get(kitSpec.getReagentId()).get(0);
			ReagentSpecVo vo = new ReagentSpecVo();
			vo.setIsLabExist(1);
			vo.setSpectId(kitSpec.getId());
			vo.setKitId(reagent.getKitId());
			vo.setReagentId(reagent.getId());
			vo.setReagentName(reagent.getName());
			vo.setReagentTypeId(reagent.getReagentTypeId());
			vo.setReagentTypeName(reagentTypeMap.get(reagent.getReagentTypeId()));
			vo.setUnit(reagent.getUnit());
			vo.setUnitName(unitMap.get(reagent.getUnit()));
			vo.setZone(reagent.getZone());
			vo.setZoneName(zoneMap.get(reagent.getZone()));
			vo.setSize(kitSpec.getSize());
			vo.setMultiple(kitSpec.getMultiple());
			vo.setFirstReagentTypeId(firstReagentTypeMap.get(reagent.getReagentTypeId()));

			// 规格比率
			BigDecimal specRatio = kitSpec.getSize().multiply(BigDecimal.valueOf(kitSpec.getMultiple()))
					.divide(kitSpec.getKitSize(), 8, RoundingMode.HALF_UP);
			vo.setSpecRatio(specRatio);
			// 组装reagent规格名称
			List<KitSpec> list = Arrays.asList(kitSpec);
			String reagentSpecName = kitSpecService.getSpecName(list, 1);
			vo.setSpecName(reagentSpecName);

			// 组装kit规格名称
			String kitSpecName = kitSpecService.getSpecName(list, 0);
			ReagentKitSpecVo rslVo = map.get(kitSpecName);
			if (rslVo == null) {
				// 组装kit信息数据
				rslVo = new ReagentKitSpecVo();
				rslVo.setIsLabExist(1);
				rslVo.setKitId(kitId);
				rslVo.setKitName(kit.getName());
				rslVo.setSpecName(kitSpecName);
				rslVo.setKitType(kit.getKitType());
				rslVo.setKitSize(kitSpec.getKitSize());
				rslVo.setUnit(kit.getUnit());
				rslVo.setUnitName(unitMap.get(kit.getUnit()));
				rslVo.setZoneName(zoneMap.get(kit.getZone()));
				rslVo.setReagentList(new ArrayList<>());
				rslVo.setQualityDay(kit.getQualityDay());
				rslVo.setFirstReagentTypeId(firstReagentTypeMap.get(kit.getReagentTypeId()));
			}
			rslVo.getReagentList().add(vo);
			map.put(kitSpecName, rslVo);

		});
		map.values().forEach(rslVo -> listVo.add(rslVo));
		return listVo;
	}

	/**
	 * 获取试剂成分规格信息列表
	 * 
	 * @param kitId
	 * @return
	 */
	public ReagentKitSpecVo getComponentSpec(Long kitId, Long[] spectIds) {
		ReagentKitSpecVo rslVo = null;
		List<KitSpec> specList = kitSpecService.selectListByIds(kitId, spectIds);
		if (specList != null && specList.size() > 0) {
			Kit kit = kitService.selectById(kitId);

			Map<Integer, String> unitMap = dictService.getUnitMap();
			Map<Integer, String> zoneMap = dictService.getZoneMap();
			Map<Long, String> reagentTypeMap = dictService.getReagentTypeMap(kit.getLabId());
			List<Reagent> reagentList = reagentService.selectList(kitId);
			Map<Long, List<Reagent>> reagentMap = reagentList.stream().collect(Collectors.groupingBy(Reagent::getId));

			List<Long> reagentTypeIds = reagentList.stream().map(Reagent::getReagentTypeId)
					.collect(Collectors.toList());
			reagentTypeIds.add(kit.getReagentTypeId());
			Map<Long, Long> firstReagentTypeMap = reagentTypeService.selectFirstReagentTypeMap(reagentTypeIds);
			for (KitSpec kitSpec : specList) {
				// 组装reagent信息数据
				Reagent reagent = reagentMap.get(kitSpec.getReagentId()).get(0);
				ReagentSpecVo vo = new ReagentSpecVo();
				vo.setIsLabExist(1);
				vo.setSpectId(kitSpec.getId());
				vo.setKitId(reagent.getKitId());
				vo.setReagentId(reagent.getId());
				vo.setReagentName(reagent.getName());
				vo.setReagentTypeId(reagent.getReagentTypeId());
				vo.setReagentTypeName(reagentTypeMap.get(reagent.getReagentTypeId()));
				vo.setUnit(reagent.getUnit());
				vo.setUnitName(unitMap.get(reagent.getUnit()));
				vo.setZone(reagent.getZone());
				vo.setZoneName(zoneMap.get(reagent.getZone()));
				vo.setSize(kitSpec.getSize());
				vo.setMultiple(kitSpec.getMultiple());
				vo.setFirstReagentTypeId(firstReagentTypeMap.get(reagent.getReagentTypeId()));

				// 规格比率
				BigDecimal specRatio = kitSpec.getSize().multiply(BigDecimal.valueOf(kitSpec.getMultiple()))
						.divide(kitSpec.getKitSize(), 8, RoundingMode.HALF_UP);
				vo.setSpecRatio(specRatio);
				// 组装reagent规格名称
				List<KitSpec> list = Arrays.asList(kitSpec);
				String reagentSpecName = kitSpecService.getSpecName(list, 1);
				vo.setSpecName(reagentSpecName);

				if (rslVo == null) {
					// 组装kit信息数据
					rslVo = new ReagentKitSpecVo();
					rslVo.setIsLabExist(1);
					rslVo.setKitId(kitId);
					rslVo.setKitName(kit.getName());
					rslVo.setSpecName(kitSpecService.getSpecName(list, 0));
					rslVo.setKitType(kit.getKitType());
					rslVo.setKitSize(kitSpec.getKitSize());
					rslVo.setUnit(kit.getUnit());
					rslVo.setUnitName(unitMap.get(kit.getUnit()));
					rslVo.setZoneName(zoneMap.get(kit.getZone()));
					rslVo.setReagentList(new ArrayList<>());
					rslVo.setQualityDay(kit.getQualityDay());
					rslVo.setFirstReagentTypeId(firstReagentTypeMap.get(kit.getReagentTypeId()));
				}
				rslVo.getReagentList().add(vo);
			}
		}

		return rslVo;
	}

	/**
	 * 删除试剂
	 * 
	 * @return
	 */
	public boolean delete(Long kitId, Long labMemberId) {
		// 1、判断是否有库存
		List<ReagentInventory> inventoryList = reagentInventoryService.selectListByKitId(kitId);
		Assert.isTrue(CollectionUtils.isEmpty(inventoryList), "存在库存不允许删除");
//		if (InventoryList != null && InventoryList.size() > 0) {
//			//删除库存
//			boolean deleteKitInventory = reagentInventoryLogic.deleteKitInventory(kitId);
//			Assert.isTrue(deleteKitInventory, "删除试剂失败");
//		}
		// 删除kit和Reagent
		boolean delKit = kitService.deleteById(kitId);
		Assert.isTrue(delKit, "删除失败");
		List<Reagent> list = reagentService.selectList(kitId);
		for (Reagent reagent : list) {
			boolean delReagent = reagentService.deleteById(reagent.getId());
			Assert.isTrue(delReagent, "删除失败");
		}
		esKitSaveService.kitSave(kitId);
		return true;
	}

	/**
	 * 删除规格时判断该规格是否有库存
	 * 
	 * @param labMemberId
	 * @param kitId
	 * @param kitSize
	 * @return
	 */
	public Integer specIsInventory(Long labMemberId, Long kitId, BigDecimal kitSize) {
		List<KitSpec> specList = kitSpecService.selectList(kitId, kitSize);
		Assert.isTrue(specList != null && specList.size() > 0, "试剂规格不存在");
		List<ReagentInventory> InventoryList = reagentInventoryService.getInventoryCountByKitSpec(specList);
		return InventoryList.size();
	}

	/**
	 * 删除规格
	 * 
	 * @param labMemberId
	 * @param kitId
	 * @param kitSize
	 * @return
	 */
	public boolean deleteSpec(Long labMemberId, Long kitId, BigDecimal kitSize) {
		Assert.isTrue(labMemberId != null && kitId != null && kitSize != null, "参数错误");
		// 1、判断是否有库存
		List<KitSpec> specList = kitSpecService.selectList(kitId, kitSize);
		Assert.isTrue(specList != null && specList.size() > 0, "试剂规格不存在");

		List<KitSpec> groupList = kitSpecService.selectSpecGroup(kitId);
		Assert.isTrue(groupList != null && groupList.size() > 1, "必须保留一个试剂规格");

		List<ReagentInventory> InventoryList = reagentInventoryService.getInventoryCountByKitSpec(specList);
		if (InventoryList != null && InventoryList.size() > 0) {
			// 判断是否是实验室所有者
			boolean isOwner = labMemberService.isOwner(specList.get(0).getLabId(), labMemberId);
			if (!isOwner) {
				for (ReagentInventory inventory : InventoryList) {
					// 判断是否有权限删除
					Assert.isTrue(inventory.getLabMemberId() == null || labMemberId.equals(inventory.getLabMemberId()),
							"无权限删除试剂");
				}
			}
			// 删除库存
			reagentInventoryService.deleteInventoryByKitSpec(specList);
		}
		// 删除规格
		for (KitSpec kitSpec : specList) {
			boolean b = kitSpecService.deleteById(kitSpec.getId());
			Assert.isTrue(b, "删除规格失败");
		}
		return true;
	}

	/**
	 * 修改试剂信息
	 * 
	 * @param pa
	 */
	public void reagentModify(KitModifyPa pa) {

		Kit kit = kitService.selectById(pa.getKitId());
		// 保存附件
		if (pa.getTempFileIds() != null && pa.getTempFileIds().length > 0) {
			fileManageService.relevanceBatch(pa.getTempFileIds(), kit.getId(), OssFileEnum.KitInstructionFile,
					kit.getLabId(), null);
		}
		MyBeanUtils.copyPropertiesNotNull(pa, kit);
		boolean k = kitService.updateById(kit);
		Assert.isTrue(k, "修改试剂失败");
		List<ReagentModifyPa> reagentList = pa.getReagentVoList();
		reagentList.forEach(reagentPa -> {
			Reagent reagent = reagentService.selectById(reagentPa.getReagentId());
			// 保存附件
			if (kit.getKitType() == 1 && reagentPa.getTempFileIds() != null && reagentPa.getTempFileIds().length > 0) {
				fileManageService.relevanceBatch(reagentPa.getTempFileIds(), reagent.getId(),
						OssFileEnum.ReagentInstructionFile, kit.getLabId(), null);
			}
			MyBeanUtils.copyPropertiesNotNull(reagentPa, reagent);
			boolean r = reagentService.updateById(reagent);
			Assert.isTrue(r, "修改试剂失败");
			if (reagent.getReagentTypeId().equals(ServiceConstant.antibody_reagent_type_id)) {
				ReagentAntibody antibody = reagentPa.getReagentAntibody();
				if (antibody != null && antibody.getId() == null) {
					ReagentAntibody antibody2 = reagentAntibodyService.selectOne(reagent.getKitId(), reagent.getId());
					if (antibody2 != null) {
						antibody.setId(antibody2.getId());
					}
				}
				if (antibody != null && antibody.getId() != null) {
					boolean a = reagentAntibodyService.updateById(antibody);
					Assert.isTrue(a, "修改试剂失败");
				} else if (antibody != null && antibody.getId() == null) {
					antibody.setKitId(kit.getId());
					antibody.setReagentId(reagent.getId());
					ReagentAntibody insert = reagentAntibodyService.insert(antibody);
					Assert.notNull(insert, "修改试剂失败");
				}

			} else if (reagent.getReagentTypeId().equals(ServiceConstant.primer_reagent_type_id)) {
				ReagentPrimer reagentPrimer = reagentPa.getReagentPrimer();

				if (reagentPrimer != null && reagentPrimer.getId() != null) {
					reagentPrimer.setKitId(kit.getId());
					reagentPrimer.setReagentId(reagent.getId());
					boolean p = reagentPrimerService.updateById(reagentPrimer);
					Assert.isTrue(p, "修改试剂失败");
					List<ReagentPrimerAttr> attrList = reagentPrimer.getPrimerAttrList();
					List<ReagentPrimerAttr> list = reagentPrimerAttrService.selectList(reagent.getKitId(),
							reagent.getId());
					if (!CollectionUtils.isEmpty(attrList)) {
						for (ReagentPrimerAttr attr : attrList) {
							attr.setKitId(reagent.getKitId());
							attr.setReagentId(reagent.getId());
							if (attr.getId() != null) {
								boolean b = reagentPrimerAttrService.updateById(attr);
								Assert.isTrue(b, "修改试剂失败");
							} else {
								if (StringUtils.isNotBlank(attr.getName()) && attr.getType() != null)
									reagentPrimerAttrService.insert(attr);
							}
						}
						// 判断哪些被删除了
						for (Iterator<ReagentPrimerAttr> iterator = list.iterator(); iterator.hasNext();) {
							ReagentPrimerAttr attr2 = iterator.next();
							for (ReagentPrimerAttr attr : attrList) {
								if (attr2.getId().equals(attr.getId())) {
									iterator.remove();
								}
							}
						}
					}
					if (!CollectionUtils.isEmpty(list)) {
						for (ReagentPrimerAttr attr2 : list) {
							reagentPrimerAttrService.deleteById(attr2.getId());
						}
					}
				} else if (reagentPrimer != null && reagentPrimer.getId() == null) {
					ReagentPrimer primer = reagentPrimerService.selectOne(reagent.getKitId(), reagent.getId());
					if (primer != null) {
						reagentPrimerService.deleteById(primer.getId());
					}
					reagentPrimer.setKitId(kit.getId());
					reagentPrimer.setReagentId(reagent.getId());
					primer = reagentPrimerService.insert(reagentPrimer);
					Assert.notNull(primer, "修改试剂失败");
					List<ReagentPrimerAttr> attrList = reagentPrimer.getPrimerAttrList();
					if (!CollectionUtils.isEmpty(attrList)) {
						for (ReagentPrimerAttr attr : attrList) {
							attr.setKitId(reagent.getKitId());
							attr.setReagentId(reagent.getId());
							if (attr.getId() != null) {
								boolean b = reagentPrimerAttrService.updateById(attr);
								Assert.isTrue(b, "修改试剂失败");
							} else {
								if (StringUtils.isNotBlank(attr.getName()) && attr.getType() != null)
									reagentPrimerAttrService.insert(attr);
							}
						}
					}
				}

			} else if (reagent.getReagentTypeId().equals(ServiceConstant.plasmid_reagent_type_id)) {
				ReagentPlasmid plasmid = reagentPa.getReagentPlasmid();
				ReagentPlasmid plasmid3 = reagentPlasmidService.selectOne(reagent.getKitId(), reagent.getId());
				if (plasmid3 != null) {
					plasmid.setId(plasmid3.getId());
				}

				if (plasmid != null && plasmid.getId() != null && plasmid.getTemplateId() == null) {
					// 5.1图谱文件
					if (plasmid.getAtlasFileIds() != null && plasmid.getAtlasFileIds().length > 0) {
						fileManageService.relevanceBatch(plasmid.getAtlasFileIds(), plasmid.getId(),
								OssFileEnum.PlasmidAtlasFile, kit.getLabId(), null);
					}
					// 测序报告文件
					if (plasmid.getReportFileIds() != null && plasmid.getReportFileIds().length > 0) {
						fileManageService.relevanceBatch(plasmid.getReportFileIds(), plasmid.getId(),
								OssFileEnum.PlasmidReportFile, kit.getLabId(), null);
					}
					// 序列文摘文件
					if (plasmid.getAbstractFileIds() != null && plasmid.getAbstractFileIds().length > 0) {
						fileManageService.relevanceBatch(plasmid.getAbstractFileIds(), plasmid.getId(),
								OssFileEnum.PlasmidAbstractFile, kit.getLabId(), null);
					}
					// 其他文件
					if (plasmid.getOtherFileIds() != null && plasmid.getOtherFileIds().length > 0) {
						fileManageService.relevanceBatch(plasmid.getOtherFileIds(), plasmid.getId(),
								OssFileEnum.PlasmidOtherFile, kit.getLabId(), null);
					}
					plasmid.setSkeletonPlasmidName(plasmid.getPlasmidName());
					boolean p = reagentPlasmidService.updateById(plasmid);
					Assert.isTrue(p, "修改试剂失败");

					List<ReagentPlasmidFragment> fragmentList = plasmid.getFragmentList();
					List<ReagentPlasmidFragment> list = reagentPlasmidFragmentService.selectList(reagent.getKitId(),
							reagent.getId());
					if (!CollectionUtils.isEmpty(fragmentList)) {
						for (ReagentPlasmidFragment fragment : fragmentList) {
							fragment.setKitId(reagent.getKitId());
							fragment.setReagentId(reagent.getId());
							if (fragment.getId() != null) {
								boolean b = reagentPlasmidFragmentService.updateById(fragment);
								Assert.isTrue(b, "修改试剂失败");
							} else {
								reagentPlasmidFragmentService.insert(fragment);
							}
						}
						// 判断哪些被删除了
						for (Iterator<ReagentPlasmidFragment> iterator = list.iterator(); iterator.hasNext();) {
							ReagentPlasmidFragment fragment2 = iterator.next();
							for (ReagentPlasmidFragment fragment : fragmentList) {
								if (fragment2.getId().equals(fragment.getId())) {
									iterator.remove();
								}
							}
						}
					}

					if (!CollectionUtils.isEmpty(list)) {
						for (ReagentPlasmidFragment fragment2 : list) {
							reagentPlasmidFragmentService.deleteById(fragment2.getId());
						}
					}
				} else if (plasmid != null && plasmid.getTemplateId() != null) {// 更改质粒模版
					ReagentPlasmid plas = reagentPlasmidService.selectOne(kit.getId(), reagent.getId());
					if (plas != null) {
						reagentPlasmidService.deleteById(plas.getId());
					}

					ReagentPlasmidUse plasmidUse = reagentPlasmidService.selectUseOne(reagent.getLabId(),
							plasmid.getTemplateId());
					if (plasmidUse != null) {
						plasmidUse.setUseTime(new Date());
						reagentPlasmidService.updateUseById(plasmidUse);
					} else {
						ReagentPlasmid plasmid2 = reagentPlasmidService.selectById(plasmid.getTemplateId());// 质粒模版数据
						if (plasmid2 != null) {
							plasmidUse = new ReagentPlasmidUse();
							plasmidUse.setLabId(kit.getLabId());
							plasmidUse.setReagentPlasmidId(plasmid2.getId());
							plasmidUse.setReagentPlasmidName(plasmid2.getPlasmidName());
							plasmidUse.setType(plasmid2.getLabId() != null ? 2 : 1);
							plasmidUse.setUseTime(new Date());
							reagentPlasmidService.insertUse(plasmidUse);
						}
					}

					plasmid.setId(null);
					plasmid.setLabId(kit.getLabId());
					plasmid.setKitId(kit.getId());
					plasmid.setReagentId(reagent.getId());
					plasmid.setPlasmidName(reagent.getName());
					plasmid = reagentPlasmidService.insert(plasmid);

					// 5.1图谱文件
					if (plasmid.getAtlasFileIds() != null && plasmid.getAtlasFileIds().length > 0) {
						fileManageService.relevanceBatch(plasmid.getAtlasFileIds(), plasmid.getId(),
								OssFileEnum.PlasmidAtlasFile, kit.getLabId(), null);
					}
					if (!CollectionUtils.isEmpty(plasmid.getAtlasFileList())) {
						for (FileManage file : plasmid.getAtlasFileList()) {
							fileManageService.insertBatch(file.getPath(), plasmid.getId(), OssFileEnum.PlasmidAtlasFile,
									kit.getLabId(), null);
						}
					}
					// 测序报告文件
					if (plasmid.getReportFileIds() != null && plasmid.getReportFileIds().length > 0) {
						fileManageService.relevanceBatch(plasmid.getReportFileIds(), plasmid.getId(),
								OssFileEnum.PlasmidReportFile, kit.getLabId(), null);
					}
					if (!CollectionUtils.isEmpty(plasmid.getReportFileList())) {
						for (FileManage file : plasmid.getReportFileList()) {
							if (file.getId() == null || file.getId() == 0)
								fileManageService.insertBatch(file.getPath(), plasmid.getId(),
										OssFileEnum.PlasmidReportFile, kit.getLabId(), null);
						}
					}
					// 序列文摘文件
					if (plasmid.getAbstractFileIds() != null && plasmid.getAbstractFileIds().length > 0) {
						fileManageService.relevanceBatch(plasmid.getAbstractFileIds(), plasmid.getId(),
								OssFileEnum.PlasmidAbstractFile, kit.getLabId(), null);
					}
					if (!CollectionUtils.isEmpty(plasmid.getAbstractFileList())) {
						for (FileManage file : plasmid.getAbstractFileList()) {
							if (file.getId() == null || file.getId() == 0)
								fileManageService.insertBatch(file.getPath(), plasmid.getId(),
										OssFileEnum.PlasmidAbstractFile, kit.getLabId(), null);
						}
					}
					// 其他文件
					if (plasmid.getOtherFileIds() != null && plasmid.getOtherFileIds().length > 0) {
						fileManageService.relevanceBatch(plasmid.getOtherFileIds(), plasmid.getId(),
								OssFileEnum.PlasmidOtherFile, kit.getLabId(), null);
					}
					if (!CollectionUtils.isEmpty(plasmid.getOtherFileList())) {
						for (FileManage file : plasmid.getOtherFileList()) {
							if (file.getId() == null || file.getId() == 0)
								fileManageService.insertBatch(file.getPath(), plasmid.getId(),
										OssFileEnum.PlasmidOtherFile, kit.getLabId(), null);
						}
					}

					List<ReagentPlasmidFragment> fragmentList = plasmid.getFragmentList();
					List<ReagentPlasmidFragment> list = reagentPlasmidFragmentService.selectList(reagent.getKitId(),
							reagent.getId());
					if (!CollectionUtils.isEmpty(fragmentList)) {
						for (ReagentPlasmidFragment fragment : fragmentList) {
							fragment.setKitId(reagent.getKitId());
							fragment.setReagentId(reagent.getId());
							if (fragment.getId() != null) {
								boolean b = reagentPlasmidFragmentService.updateById(fragment);
								Assert.isTrue(b, "修改试剂失败");
							} else {
								reagentPlasmidFragmentService.insert(fragment);
							}
						}
						// 判断哪些被删除了
						for (Iterator<ReagentPlasmidFragment> iterator = list.iterator(); iterator.hasNext();) {
							ReagentPlasmidFragment fragment2 = iterator.next();
							for (ReagentPlasmidFragment fragment : fragmentList) {
								if (fragment2.getId().equals(fragment.getId())) {
									iterator.remove();
								}
							}
						}
					}

					if (!CollectionUtils.isEmpty(list)) {
						for (ReagentPlasmidFragment fragment2 : list) {
							reagentPlasmidFragmentService.deleteById(fragment2.getId());
						}
					}

				} else if (plasmid != null && plasmid.getId() == null) {

					plasmid.setId(null);
					plasmid.setLabId(kit.getLabId());
					plasmid.setKitId(kit.getId());
					plasmid.setReagentId(reagent.getId());
					plasmid.setPlasmidName(reagent.getName());
					plasmid = reagentPlasmidService.insert(plasmid);
					// 5.1图谱文件
					if (plasmid.getAtlasFileIds() != null && plasmid.getAtlasFileIds().length > 0) {
						fileManageService.relevanceBatch(plasmid.getAtlasFileIds(), plasmid.getId(),
								OssFileEnum.PlasmidAtlasFile, kit.getLabId(), null);
					}
					// 测序报告文件
					if (plasmid.getReportFileIds() != null && plasmid.getReportFileIds().length > 0) {
						fileManageService.relevanceBatch(plasmid.getReportFileIds(), plasmid.getId(),
								OssFileEnum.PlasmidReportFile, kit.getLabId(), null);
					}
					// 序列文摘文件
					if (plasmid.getAbstractFileIds() != null && plasmid.getAbstractFileIds().length > 0) {
						fileManageService.relevanceBatch(plasmid.getAbstractFileIds(), plasmid.getId(),
								OssFileEnum.PlasmidAbstractFile, kit.getLabId(), null);
					}
					// 其他文件
					if (plasmid.getOtherFileIds() != null && plasmid.getOtherFileIds().length > 0) {
						fileManageService.relevanceBatch(plasmid.getOtherFileIds(), plasmid.getId(),
								OssFileEnum.PlasmidOtherFile, kit.getLabId(), null);
					}
					List<ReagentPlasmidFragment> fragmentList = plasmid.getFragmentList();
					if (!CollectionUtils.isEmpty(fragmentList)) {
						for (ReagentPlasmidFragment fragment : fragmentList) {
							fragment.setKitId(reagent.getKitId());
							fragment.setReagentId(reagent.getId());
							if (fragment.getId() != null) {
								boolean b = reagentPlasmidFragmentService.updateById(fragment);
								Assert.isTrue(b, "修改试剂失败");
							} else {
								reagentPlasmidFragmentService.insert(fragment);
							}
						}
					}
				}
			}

		});
		esKitSaveService.kitSave(pa.getKitId());
	}

}
